package com.appspot.zarm.game;

import java.util.ArrayList;
import java.util.List;

import com.appspot.zarm.controller.user.User;
import com.appspot.zarm.transfer.GamesList;
import com.appspot.zarm.transfer.GamesListExternalizer;

/**
 * This is a storage of all games. You can get any of the game with sessionId of the player.
 * To add new Game you need to have at least one sessionId. 
 * @author Zhardzetski
 *
 */
public class GamesStorage implements GamesListExternalizer {

	private List<Game> games = new ArrayList<Game>();

	public Player getPlayer(User user){
		for (Game game : games) {
			Player player = game.getPlayerById( user.getId() );
			if (player != null) return player;
		}
		return null;
	}
	
	public Game getGame(Player player) {
		for (Game game : games) {
			if (game.hasPlayer(player)) {
				return game;
			}
		}
		return null;
	}

	public void removeGame(Player player) {
		Game game = getGame(player);
		games.remove(game);
	}

	public void addGame(Game game) {
		games.add(game);
	}

	public boolean hasPlayer(Player player) {
		if (getGame(player) != null) {
			return true;
		}
		return false;
	}
	
	public GamesList getGamesList() {
		List<String> list = new ArrayList<String>();
		for (Game game: games) {
			list.add(game.getPlayerOne().getId());
		}
		GamesList result = new GamesList();
		result.setList(list);
		return result;
	}
	
	public void save() {
		// empty
	}
	
	public void load(String fileName) {
		// empty
	}

}
